package com.zst.bluetooth.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

import com.baidu.mobstat.StatService;
import com.zst.bluetooth.bean.BlueMessage;
import com.zst.bluetooth.bean.BluetoothMsg;
import com.zst.bluetooth.task.ConnectTask;

import org.joda.time.DateTime;

/**
 * Created by zhang on 2016/3/16 0016. 短信广播接收者
 */
public class SMSReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
        //判断收到的广播是否是短信接收的广播
        if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
            //获取intent参数
            Bundle bundle = intent.getExtras();
            //判断bundle内容
            if (bundle != null) {
                //取pdus内容,转换为Object[]
                Object[] pdus = (Object[]) bundle.get("pdus");
                //解析短信
                SmsMessage[] messages = new SmsMessage[pdus.length];
                StatService.onEvent(context, "receive_sms", "接收到短信");
                for (int i = 0; i < messages.length; i++) {
                    byte[] pdu = (byte[]) pdus[i];
                    messages[i] = SmsMessage.createFromPdu(pdu);
                }
                //解析完内容后分析具体参数
                for (SmsMessage msg : messages) {
                    //获取短信内容
                    String content = msg.getMessageBody();//短信内容
                    String sender = msg.getOriginatingAddress();//发送人
                    DateTime date = new DateTime(msg.getTimestampMillis());//短信时间
                    final BlueMessage message = new BlueMessage();
                    message.content = content;
                    message.belong = BluetoothMsg.blueToothAddress;
                    message.sender = sender;
                    message.createTime = date.getMillis();
                    message.type = BlueMessage.Type.TYPE_SMS;
                    message.state = BlueMessage.State.STATE_PRE_SEND;

                    ConnectTask task = new ConnectTask("SMS:" + content + "#" + sender + "#" + date.toString("YYYY/MM/dd HH:mm"));
                    task.setTaskInfo(new TaskInfo() {
                        @Override
                        public void onPre() {
                            message.state = BlueMessage.State.STATE_SENDING;
                            StatService.onEvent(context, "send_sms", "短信发送");
                        }

                        @Override
                        public void onFinish(String result) {
                            if (result.equals("success")) {
                                message.state = BlueMessage.State.STATE_SEND_SUCCESS;
                                App.getOrm().save(message);
                                StatService.onEvent(context, "send_sms_success", "短信成功");
                            } else {
                                message.state = BlueMessage.State.STATE_SEND_FAIL;
                                StatService.onEvent(context, "send_sms_fail", "短信失败");
                            }
                        }
                    });
                    //如果已经连接,则执行发送
                    if (BlueToothUtils.isOpen()) {
                        task.start();
                    }

                }

            }
        }
    }
}
